#include<cstdio>//uncle-lu
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

int F[300][300];
int ans, n;

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) 
		read(F[i][i]);

	for (int len = 2; len <= n; len++) 
	{
		for (int l = 1; l <= n-len+1; l++) 
		{
			int r = l + len - 1;
			for (int k = l; k <= r-1; k++) 
				if(F[l][k] == F[k+1][r])
					F[l][r] = std::max(F[l][r], F[l][k] + 1);
			ans = std::max(ans, F[l][r]);
		}
	}

	printf("%d", ans);
	return 0;
}
